﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using stockfund.src;
using stockfund.popform;
using MySQLDriverCS;
using System.Data.Common;
using System.Data.SqlClient;
using System.Diagnostics;
namespace stockfund
{
    public partial class frmMain : Form
    {
        private MySQLConnection conn;
        public frmMain()
        {
            InitializeComponent();
            this.conn = Tools.getConn();            
        }

        private void main_Load(object sender, EventArgs e)
        {
            loadStockList();
        }
        
        private void loadStockList()
        {
            string sql = "select distinct(code),name from okzcggstat";
            MySQLCommand cmd = new MySQLCommand("set names gbk", conn);
            cmd.ExecuteNonQuery();
            cmd = new MySQLCommand(sql, conn);

            MySQLDataAdapter myadp = new MySQLDataAdapter(cmd);
            DataSet ds = new DataSet();
            myadp.Fill(ds, "stocklist");
            this.dgv_stocklist.DataSource = ds.Tables["stocklist"];
            
            this.dgv_stocklist.Columns[0].Width = 60;
            this.dgv_stocklist.Columns[1].Width = 90;
            this.dgv_stocklist.Visible = true;
            this.dgv_stocklist.Columns[0].Name = "code";
            this.dgv_stocklist.Columns[1].Name = "name";
            this.dgv_stocklist.Columns[0].HeaderText = "代码";
            this.dgv_stocklist.Columns[1].HeaderText = "名称";
            Stock stock;
            foreach (DataRow row in ds.Tables["stocklist"].Rows)
            {
                stock = new Stock();
                
                stock.code = row["code"].ToString();
                stock.name = row["name"].ToString();
                stock.chnCode = Tools.GetPYString(stock.name);
                GLOBAL_DATA.STOCKS.Add(stock);
            }

            sql = "select code,name,sum(range)range,sum(liutongbi)liutongbi,sum(days)days ,sum(fund)fund,atype from okzcggstat z group by z.code,z.atype order by sum(days) desc,sum(range) desc ,sum(liutongbi) desc";
            cmd.ExecuteNonQuery();
            cmd = new MySQLCommand(sql, conn);
            myadp = new MySQLDataAdapter(cmd);
            myadp.Fill(ds, "_okzcgglist");
            OkZcgg okzcgg;
            DataTable _dt = new DataTable("okzcgglist");
            _dt.Columns.Add("code", System.Type.GetType("System.String"));
            _dt.Columns.Add("name", System.Type.GetType("System.String"));
            _dt.Columns.Add("range", System.Type.GetType("System.Double"));
            _dt.Columns.Add("liutongbi", System.Type.GetType("System.Double"));
            _dt.Columns.Add("days", System.Type.GetType("System.Double"));
            _dt.Columns.Add("fund", System.Type.GetType("System.Double"));
            _dt.Columns.Add("atype", System.Type.GetType("System.String"));
            foreach (DataRow row in ds.Tables["_okzcgglist"].Rows)
            {
                DataRow _dr = _dt.NewRow();
                okzcgg = new OkZcgg();
                okzcgg.code = row["code"].ToString();
                okzcgg.name = row["name"].ToString();
                okzcgg.range = double.Parse(row["range"].ToString());
                okzcgg.liutongbi = double.Parse(row["liutongbi"].ToString());
                okzcgg.days = double.Parse(row["days"].ToString());
                okzcgg.fund = double.Parse(row["fund"].ToString());
                okzcgg.chnCode = Tools.GetPYString(okzcgg.name);
                string atype = row["atype"].ToString();
                if (atype == "lianxujigouzengcanggegu")
                {
                    okzcgg.atype = "连续机构增仓";
                }
                else
                {
                    okzcgg.atype = "连续机构减仓";
                }
                GLOBAL_DATA.OKZCGG.Add(okzcgg);
                _dr[0] = okzcgg.code;
                _dr[1] = okzcgg.name;
                _dr[2] = okzcgg.range;
                _dr[3] = okzcgg.liutongbi;
                _dr[4] = okzcgg.days;
                _dr[5] = okzcgg.fund;
                _dr[6] = okzcgg.atype;
                _dt.Rows.Add(_dr);
            }
            
            

            this.dgv_okzcgglist.DataSource = _dt;
            this.dgv_okzcgglist.Columns[0].Name = "code";
            this.dgv_okzcgglist.Columns[1].Name = "name";
            this.dgv_okzcgglist.Columns[2].Name = "range";
            this.dgv_okzcgglist.Columns[3].Name = "liutongbi";
            this.dgv_okzcgglist.Columns[4].Name = "days";
            this.dgv_okzcgglist.Columns[5].Name = "fund";
            this.dgv_okzcgglist.Columns[6].Name = "atype";

            this.dgv_okzcgglist.Columns[0].HeaderText = "代码";
            this.dgv_okzcgglist.Columns[1].HeaderText = "名称";
            this.dgv_okzcgglist.Columns[2].HeaderText = "区间涨幅";
            this.dgv_okzcgglist.Columns[3].HeaderText = "流通比";
            this.dgv_okzcgglist.Columns[4].HeaderText = "增仓天数";
            this.dgv_okzcgglist.Columns[5].HeaderText = "增仓资金";
            this.dgv_okzcgglist.Columns[6].HeaderText = "类型";

            this.dgv_okzcgglist.Columns[0].Width = 48;
            this.dgv_okzcgglist.Columns[1].Width = 60;
            this.dgv_okzcgglist.Columns[2].Width = 60;
            this.dgv_okzcgglist.Columns[3].Width = 50;
            this.dgv_okzcgglist.Columns[4].Width = 67;
            this.dgv_okzcgglist.Columns[5].Width = 70;

        }
        private void tsmi_getData_Click(object sender, EventArgs e)
        {
            frmGetData wnd = new frmGetData(conn);
            wnd.ShowInTaskbar = false;
            wnd.ShowDialog();
        }

        private void tsmi_about_Click(object sender, EventArgs e)
        {
            frmAbout about = new frmAbout();
            about.ShowInTaskbar = false;
            about.ShowDialog();
        }

        private void tsmi_content_Click(object sender, EventArgs e)
        {
            frmContent wnd = new frmContent();
            wnd.ShowInTaskbar = false;
            wnd.ShowDialog();
        }

        private void tsmi_statdata_Click(object sender, EventArgs e)
        {
            this.sysStatus.Text = Tools.checkData(this.conn,"zcggstat").ToString();
            
        }

        private void tsmi_test_click(object sender, EventArgs e)
        {
            frmTesting test = new frmTesting();
            test.ShowInTaskbar = false;
            test.ShowDialog();
               
        }

        private void dgv_stocklist_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.dgv_stocklist.Rows[this.dgv_stocklist.CurrentCell.RowIndex].Selected = true;
            DataGridViewRow row = this.dgv_stocklist.Rows[this.dgv_stocklist.CurrentCell.RowIndex];
            string code = row.Cells[0].Value.ToString();
            string name = row.Cells[1].Value.ToString();
            this.lbl_stockname.Text = name + "["+code+"]";
            DataSet ds = new DataSet();


            DataTable dtOkzcgglist = new DataTable("okzcgglist");
            DataRow drOkzcgg;
            dtOkzcgglist.Columns.Add("code", System.Type.GetType("System.String"));
            dtOkzcgglist.Columns.Add("name", System.Type.GetType("System.String"));
            dtOkzcgglist.Columns.Add("range", System.Type.GetType("System.Double"));
            dtOkzcgglist.Columns.Add("liutongbi", System.Type.GetType("System.Double"));
            dtOkzcgglist.Columns.Add("days", System.Type.GetType("System.Double"));
            dtOkzcgglist.Columns.Add("fund", System.Type.GetType("System.Double"));
            dtOkzcgglist.Columns.Add("atype", System.Type.GetType("System.String"));
            int i = 0;

            foreach (OkZcgg okzcgg in GLOBAL_DATA.OKZCGG)
            {
                if (okzcgg.chnCode.IndexOf(code) > -1 || okzcgg.code.IndexOf(code) > -1)
                {
                    drOkzcgg = dtOkzcgglist.NewRow();
                    drOkzcgg[0] = okzcgg.code;
                    drOkzcgg[1] = okzcgg.name;
                    drOkzcgg[2] = okzcgg.range;
                    drOkzcgg[3] = okzcgg.liutongbi;
                    drOkzcgg[4] = okzcgg.days;
                    drOkzcgg[5] = okzcgg.fund;
                    drOkzcgg[6] = okzcgg.atype;
                    dtOkzcgglist.Rows.Add(drOkzcgg);
                    i++;
                }
            }
            this.dgv_okzcgglist.DataSource = dtOkzcgglist;

            
            string sql = "select days,fund,qujianbi,liutongbi,swap,range,starttime,endtime from okzcggstat z where code='"+code+"' order by id desc";
            MySQLCommand cmd = new MySQLCommand("set names gbk", conn);
            cmd.ExecuteNonQuery();
            cmd = new MySQLCommand(sql, conn);
            MySQLDataAdapter myadp = new MySQLDataAdapter(cmd);
            myadp.Fill(ds, "zcgglist");



            sql = "select round(avg(price),2)price,sum(big)big,sum(small)small,atype,period from boYistat where code='" + code + "' group by atype,period order by id desc";
            cmd = new MySQLCommand("set names gbk", conn);
            cmd.ExecuteNonQuery();
            cmd = new MySQLCommand(sql, conn);
            myadp = new MySQLDataAdapter(cmd);
            myadp.Fill(ds, "boyilist");

            
            sql = "select fund,percent,atype,period,statdate from fundstat where code='" + code + "' order by id desc";
            cmd = new MySQLCommand("set names gbk", conn);
            cmd.ExecuteNonQuery();
            cmd = new MySQLCommand(sql, conn);
            myadp = new MySQLDataAdapter(cmd);
            myadp.Fill(ds, "fundlist");
            
            
        }

        private void tbx_code_TextChanged(object sender, EventArgs e)
        {
            string code = Tools.GetPYString((sender as TextBox).Text);
            DataTable dtStocklist = new DataTable("stocklist");
            DataRow drStocklist;
            dtStocklist.Columns.Add("code",System.Type.GetType("System.String"));
            dtStocklist.Columns.Add("name",System.Type.GetType("System.String"));
            int i = 0;
            foreach(Stock stock in GLOBAL_DATA.STOCKS)
            {
                if (stock.chnCode.IndexOf(code) > -1 || stock.code.IndexOf(code) > -1)
                {
                    drStocklist = dtStocklist.NewRow();
                    drStocklist[0] = stock.code;
                    drStocklist[1] = stock.name;
                    dtStocklist.Rows.Add(drStocklist);
                    i++;
                }
            }
            this.dgv_stocklist.DataSource = dtStocklist;
        }

        private void tsmi_fund_Click(object sender, EventArgs e)
        {
            frmFund fund = new frmFund();
            fund.ShowDialog();
            fund.ShowInTaskbar = false;
        }

        private void tsmi_boyi_Click(object sender, EventArgs e)
        {
            frmBoyi boyi = new frmBoyi();
            boyi.ShowDialog();
            boyi.ShowInTaskbar = false;
        }

        private void tsmi_zcgg_Click(object sender, EventArgs e)
        {
            frmZcgg zcgg = new frmZcgg();
            zcgg.ShowDialog();
            zcgg.ShowInTaskbar = false;
        }

        private void tsmi_getstocklist_Click(object sender, EventArgs e)
        {
            
            MySQLCommand cmd = new MySQLCommand("set names gbk", conn);
            cmd.ExecuteNonQuery();
            MySQLDataAdapter myadp;
            DataSet ds = new DataSet();
            string tsql = "select distinct(code),name from ";
            string sql = "";
            int i = 0;
            foreach (string table in GLOBAL_DATA.STOCKSLIST_TABLES)
            {
                if (i == 0)
                {
                    sql = sql + tsql + table; 
                }
                else
                {
                    sql = sql + " union " + tsql + table;
                }
                i++;
            }
            sql = tsql + "(" + sql + ") a ";
            cmd = new MySQLCommand(sql, conn);
            myadp = new MySQLDataAdapter(cmd);
            myadp.Fill(ds, "stocklist");
            this.dgv_stocklist.DataSource = ds.Tables["stocklist"];
            Stock stock;
            GLOBAL_DATA.STOCKS.Clear();
            foreach (DataRow row in ds.Tables["stocklist"].Rows)
            {
                stock = new Stock();

                stock.code = row["code"].ToString();
                stock.name = row["name"].ToString();
                stock.chnCode = Tools.GetPYString(stock.name);
                GLOBAL_DATA.STOCKS.Add(stock);
            }
            
            MessageBox.Show("股票列表已经更新！\n\n\n当前统计数据中有"+GLOBAL_DATA.STOCKS.Count.ToString()+"只股票！","提示：",MessageBoxButtons.OK);
        }

        private void tsmi_restart_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("要重新启动嘛？", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string strAppFileName = Process.GetCurrentProcess().MainModule.FileName;
                Process myNewProcess = new Process();
                myNewProcess.StartInfo.FileName = strAppFileName;
                myNewProcess.StartInfo.WorkingDirectory = Application.ExecutablePath;
                myNewProcess.Start();
                Application.Exit();
            }
            //System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location); 
        }

        private void tsmi_exit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("要退出软件嘛？", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void tsmi_hangyestat_Click(object sender, EventArgs e)
        {
            frmHangye hangye =  new frmHangye();
            hangye.ShowDialog();
            hangye.ShowInTaskbar = false;
           

        }
    }
}
